name: Go

on:
  push:
    branches: 
      - 'main'
    tags:
      - '*'
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:

  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    
    steps:
    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: 1.16

    - name: Build
      run: go build -v ./...

    - name: Test
      run: go test ./...

    - name: Lint
      uses: golangci/golangci-lint-action@v4
      with:
        version: v1.54

    - name: Set up QEMU
      uses: docker/setup-qemu-action@v3
      
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Log in to the Container registry
      uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Extract metadata (tags, labels) for Docker
      id: meta
      uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=edge,branch=main
          type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr
          type=semver,pattern={{version}}
          type=semver,pattern={{major}}.{{minor}}
 
    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        context: .
        file: Dockerfile.gitdiff2fly
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
